package com.fitness.front.aop.dw;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.fitness.front.aop.LogMessage;
import com.fitness.front.modal.course.vo.CourseVO;
import com.fitness.front.modal.datavisual.MemberBehavior;
import com.fitness.front.modal.response.ServerResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Aspect
@Component
public class CoursePVAspect {

    @Pointcut("execution(* com.fitness.front.controller.CourseController.getCourseForm(..))")
    public void coursePVLogPointcut(){}

    @Around("coursePVLogPointcut()")
    public Object coursePVLog(ProceedingJoinPoint point) throws Throwable{
        try {
            Object result = point.proceed();
//            String memberId = StpUtil.getLoginIdAsString();
            CourseVO courseVO = ((ServerResponseEntity<CourseVO>)result).getData();
            MemberBehavior memberBehavior = new MemberBehavior();
            memberBehavior.setCourseId(courseVO.getId());
            memberBehavior.setCategoryId(courseVO.getCategoryId());
            memberBehavior.setBehaviorType(MemberBehavior.PV);
//            memberBehavior.setMemberId(Long.parseLong(memberId));
            memberBehavior.setTimestamp(System.currentTimeMillis());
            LogMessage<MemberBehavior> logMessage = new LogMessage<>(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),
                    LogMessage.COURSE_PV_TYPE,memberBehavior);
            log.info("{}", JSONUtil.toJsonStr(logMessage));
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
